package cn.gson.oasys.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

public class ClientUtil {

	/**
	 * 获取客户端的IP
	 * 
	 * @param request
	 *            HttpServletRequest对象
	 * @return 客户端的IP
	 */
	public static String getClientIp(HttpServletRequest request) {
		String remoteAddr = "";
		if (request != null) {
			remoteAddr = request.getHeader("X-FORWARDED-FOR");
			if (remoteAddr == null || "".equals(remoteAddr)) {
				remoteAddr = request.getRemoteAddr();
			}
		}
		return remoteAddr;
	}

	/**
	 * 此方法调用百度AIP来查询IP所在地域(YYR)
	 * 
	 * @param strIP（传入的IP地址）
	 * @return
	 */
	public static String getAddressByIP(String strIP) {
		try {
			URL url = new URL("http://api.map.baidu.com/location/ip?ak=F454f8a5efe5e577997931cc01de3974&ip=" + strIP);
			URLConnection conn = url.openConnection();
			BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
			String line = null;
			StringBuffer result = new StringBuffer();
			while ((line = reader.readLine()) != null) {
				result.append(line);
			}
			reader.close();
			String ipAddr = result.toString();
			try {
				JSONObject obj1 = JSON.parseObject(ipAddr);
				if ("0".equals(obj1.get("status").toString())) {
					JSONObject obj2 = JSON.parseObject(obj1.get("content").toString());
					JSONObject obj3 = JSON.parseObject(obj2.get("address_detail").toString());
					return obj3.get("city").toString();
				} else {
					return "读取失败";
				}
			} catch (JSONException e) {
				e.printStackTrace();
				return "读取失败";
			}
		} catch (IOException e) {
			return "读取失败";
		}
	}

	public static void main(String[] args) {
		String[] strIPs = new String[] { "119.33.194.195", "112.96.70.32" };
		for (String strIP : strIPs) {
			String str = getAddressByIP(strIP);
			System.out.println(str);
		}
	}
}
